class InvoicesController < ApplicationController
  # GET /invoices
  # GET /invoices.xml
  
  before_filter :login_required
  before_filter :session_expiry
  
  private
  def set_positions_from_cookie
	position_strings = cookies[:positions].split(',')
	position_strings.each do |position_string|
	  position_data = position_string.split('_')
      new_position = DocumentsPosition.new
	  new_position.document_id = @invoice.id
	  new_position.product_id = position_data[0]
	  new_position.quantity = position_data[1]
	  @invoice.positions << new_position
	end
  end
  
  public
  def index
    @invoices = Invoice.all
    
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @invoices }
    end
  end

  # GET /invoices/1
  # GET /invoices/1.xml
  def show
    @invoice = Invoice.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @invoice }
    end
  end

  # GET /invoices/new
  # GET /invoices/new.xml
  def new
    @invoice = Invoice.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @invoice }
    end
  end

  # GET /invoices/1/edit
  def edit
    @invoice = Invoice.find(params[:id])
  end

  def correct
    old_invoice = Invoice.find(params[:id])
	@invoice = Invoice.new
	@invoice.corrected_invoice = old_invoice
	@invoice.source = old_invoice.source
	@invoice.target = old_invoice.target
	@invoice.selling_date = old_invoice.selling_date
	@invoice.due_date = old_invoice.due_date
	@invoice.payment_method = old_invoice.payment_method
	@invoice.notes = old_invoice.notes
	@invoice.positions = old_invoice.positions
  end

  # POST /invoices
  # POST /invoices.xml
  def create
    @invoice = Invoice.new(params[:invoice])
	@invoice.document_type = "invoice"
	@invoice.issue_date = Time.now
	
	set_positions_from_cookie
    
    respond_to do |format|
      if @invoice.save
        flash[:notice] = 'Invoice was successfully created.'
        format.html { redirect_to(@invoice) }
        format.xml  { render :xml => @invoice, :status => :created, :location => @invoice }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @invoice.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /invoices/1
  # PUT /invoices/1.xml
  def update
    @invoice = Invoice.find(params[:id])
	
	@invoice.positions = []
    
    set_positions_from_cookie
    
    respond_to do |format|
      if @invoice.update_attributes(params[:invoice])
        flash[:notice] = 'Invoice was successfully updated.'
        format.html { redirect_to(@invoice) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @invoice.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /invoices/1
  # DELETE /invoices/1.xml
  def destroy
    @invoice = Invoice.find(params[:id])
    @invoice.destroy

    respond_to do |format|
      format.html { redirect_to(invoices_url) }
      format.xml  { head :ok }
    end
  end

  def filter
    @query = ""
    @param_table = []
    
    @query = @query + "issue_date > ? "
    @param_table << prepare_from_date("issue_date")
    @query = @query + "AND issue_date < ? "
    @param_table << prepare_to_date("issue_date")
	
	@query = @query + "AND number like ? "
    @param_table << "%"+params[:filter][:number]+"%"
	
	@query = @query + "AND corrected_invoice_id like ? "
    @param_table << "%"+params[:filter][:corrects]+"%"
    
    if params[:filter][:source] != ""
        @query = @query + "AND source_id = ? "
        @param_table << params[:filter][:source]
    end
    
    if params[:filter][:target] != "" 
        @query = @query + "AND target_id = ? "
        @param_table << params[:filter][:target]
    end
    
    @query = @query + "AND selling_date > ? "
    @param_table << prepare_from_date("selling_date")
    @query = @query + "AND selling_date < ? "
    @param_table << prepare_to_date("selling_date")
    
    @query = @query + "AND due_date > ? "
    @param_table << prepare_from_date("due_date")
    @query = @query + "AND due_date < ? "
    @param_table << prepare_to_date("due_date")
    
    @query = @query + "AND payment_method like ? "
    @param_table << "%"+params[:filter][:payment_method]+"%"
    
    @query = @query + "AND notes like ? "
    @param_table << "%"+params[:filter][:notes]+"%"
    
    if params[:filter][:creator] != ""
        @query = @query + "AND employee_id = ? "
        @param_table << params[:filter][:creator]
    end
    
    @invoices = Invoice.find(:all, :conditions => [@query, *@param_table])
  end
end
